{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "import time\n",
    "import datetime\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import math\n",
    "from math import radians, cos, sin, asin, sqrt \n",
    "import random\n",
    "\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.ticker import MultipleLocator, FormatStrFormatter\n",
    "from matplotlib.patches import Ellipse, Circle\n",
    "\n",
    "ROOTDIR = os.path.abspath(os.path.realpath('./')) + '/Py'\n",
    "\n",
    "sys.path.append(os.path.join(ROOTDIR, ''))\n",
    "\n",
    "import dgckernel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "Zoom=13\n",
    "CALCULATOR = dgckernel.Calculator()\n",
    "CALCULATOR.SetLayer(Zoom)\n",
    "\n",
    "'''Boundary verify'''\n",
    "\n",
    "def GIS_verify(boudary_set,lat,lng):\n",
    "    tag=False\n",
    "    i=-1\n",
    "    j=len(boudary_set)-1\n",
    "    max_lng=max(boudary_set[:,1]);min_lng=min(boudary_set[:,1]);\n",
    "    max_lat=max(boudary_set[:,0]);min_lat=min(boudary_set[:,0]);\n",
    "    \n",
    "    if lng>max_lng or lng<min_lng or lat>max_lat or lat<min_lat:\n",
    "        return tag\n",
    "    \n",
    "    while i<len(boudary_set)-1:\n",
    "        i += 1\n",
    "        if (boudary_set[j][0]<=lat and lat<=boudary_set[i][0]) or (boudary_set[i][0]<=lat and lat<=boudary_set[j][0]):\n",
    "            if (lng<(boudary_set[j][1]-boudary_set[i][1])*(lat-boudary_set[i][0])/(boudary_set[j][0]-boudary_set[i][0])+boudary_set[i][1]):\n",
    "                tag=not tag\n",
    "        j=i \n",
    "    return tag\n",
    "    \n",
    "\n",
    "'''GRID ID'''\n",
    "\n",
    "def get_grid(lng,lat):\n",
    "\n",
    "    return CALCULATOR.HexCellKey(dgckernel.GeoCoord(lat, lng))\n",
    "\n",
    "'''GRID SHAPE'''\n",
    "\n",
    "def get_grid_shape(grid):\n",
    "\n",
    "    return CALCULATOR.HexCellVertexesAndCenter(grid)\n",
    "\n",
    "'''Neighbor Grid'''\n",
    "\n",
    "def grid_neighbor(grid, low_layer, up_layer):\n",
    "\n",
    "    neighbors = CALCULATOR.HexCellNeighbor(grid, up_layer)\n",
    "    _neighbors = CALCULATOR.HexCellNeighbor(grid, low_layer)\n",
    "    neighbors = [e for e in neighbors if e not in _neighbors]\n",
    "    return neighbors\n",
    "\n",
    "'''GRID ELIMINATE'''\n",
    "\n",
    "def grid_eliminate(grid_list,boudary_set):\n",
    "    grid_result=list()\n",
    "    for grid in grid_list:\n",
    "        v_f,c_f=get_grid_shape(grid)\n",
    "        c_lng,c_lat=c_f.lng,c_f.lat;\n",
    "        if GIS_verify(boudary_set,c_lat,c_lng):\n",
    "            grid_result.append(grid)\n",
    "    return grid_result"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 01 经纬度转化成格子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'OL13F1i6599j1654'"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "lat,lng=30.671824,104.066923\n",
    "\n",
    "get_grid(lng,lat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 02 格子获取其边界"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "104.07300862352109 30.666944569109035\n",
      "104.07709703768677 30.671111256179387\n",
      "104.07505666608537 30.676759346393993\n",
      "104.06892749096193 30.67824014084931\n",
      "104.06483960990455 30.674073071408756\n",
      "104.06688037076711 30.66842558986273\n",
      "104.07096817853132 30.672592365415845\n"
     ]
    }
   ],
   "source": [
    "grid='OL13F1i6599j1654'\n",
    "\n",
    "v_f,c_f=get_grid_shape(grid)\n",
    "\n",
    "\n",
    "'''v_f 为格子的顶点'''\n",
    "\n",
    "'''v_c 为格子的中心点'''\n",
    "\n",
    "print(v_f[0].lng,v_f[0].lat)\n",
    "\n",
    "print(v_f[1].lng,v_f[1].lat)\n",
    "\n",
    "print(v_f[2].lng,v_f[2].lat)\n",
    "\n",
    "print(v_f[3].lng,v_f[3].lat)\n",
    "\n",
    "print(v_f[4].lng,v_f[4].lat)\n",
    "\n",
    "print(v_f[5].lng,v_f[5].lat)\n",
    "\n",
    "print(c_f.lng,c_f.lat)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 03 获取格子的外层的格子"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['OL13F1i6600j1654',\n",
       " 'OL13F1i6600j1653',\n",
       " 'OL13F1i6599j1653',\n",
       " 'OL13F1i6598j1654',\n",
       " 'OL13F1i6598j1655',\n",
       " 'OL13F1i6599j1655']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "grid='OL13F1i6599j1654'\n",
    "\n",
    "low_layer=0\n",
    "\n",
    "'''向外围获取一层'''\n",
    "up_layer=1\n",
    "\n",
    "grid_neighbor(grid, low_layer, up_layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
